package com.ruoyi.jihuo.api;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.RuoYiApplication;
import com.ruoyi.jihuo.domain.JhTmAccount;
import com.ruoyi.jihuo.domain.JhTmTemplate;
import com.ruoyi.jihuo.mapper.JhTmTaskMapper;
import com.ruoyi.jihuo.service.IJhTmTaskService;
import com.ruoyi.jihuo.service.IJhTmTemplateService;
import com.taobao.top.schema.exception.TopSchemaException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@SpringBootTest(classes = RuoYiApplication.class)
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
public class CallTaoApiServiceTest2 {

    @Autowired
    private UploadImageApi uploadImageApi;

    @Resource
    private CallTaoApiService callTaoApiService;

    private JhTmAccount jhTmAccount;

    @Autowired
    private IJhTmTaskService jhTmTaskService;

    @Autowired
    private JhTmTaskMapper jhTmTaskMapper;

    @Autowired
    private IJhTmTemplateService jhTmTemplateService;

    @Before
    public void setUp() {
        jhTmAccount = new JhTmAccount();
        jhTmAccount.setJhName("奴风灼烨专卖店");
        jhTmAccount.setJhAppKey("35070019");
        jhTmAccount.setJhBrand("2343935516");
        jhTmAccount.setJhAppSecret("ad4719fd51eeec1d35ac3d0ad7787d84");
        jhTmAccount.setJhAppSession("610010229d1bf5d700e04eeab7a0ca5e17871e099ce61d32219531158175");
        jhTmAccount.setJhTrafficNum(1000);
        JhTmTemplate jhTmTemplate = jhTmTemplateService.selectJhTmTemplateByJhId(3123123L);
        // 包邮
        jhTmTemplate.setJhIsPost("1");
        // 违规
        jhTmTemplate.setJhIsGotOut("1");
        // 无sku
        jhTmTemplate.setJhIsSku("1");
        List<String> jhClass = new ArrayList<>();
        jhClass.add("121370008");
        jhTmTemplate.setJhClass(jhClass);
        jhTmTemplate.setJhIsCopyColor("1");
        jhTmTemplate.setJhDescMode("1");
        jhTmTemplate.setJhPcTopNum(0);
        jhTmTemplate.setJhPcBottomNum(0);
        jhTmTemplate.setJhPcReserveNum(999);
        jhTmTemplate.setJhPrimaryPictureMode("Horizontal");
        jhTmTemplate.setJhWatermarkScope("ABC");
        jhTmTemplate.setJhPictureCompressibility(99);
        jhTmTemplate.setJhWatermarkType("Text");
        jhTmTemplate.setJhWatermarkWord("奴风");
        jhTmTemplate.setJhWatermarkPlace("TOP_LEFT");
        jhTmTemplate.setJhWatermarkTransparence(99);
        //jhTmTemplate.setJhIsFirstPictureReplace("1");
        jhTmTemplateService.updateJhTmTemplate(jhTmTemplate);
    }

    @Test
    public void allChainTest() {
        jhTmTaskService.start(1929467046697066498L);
    }



    @Test
    public void getItemsAuthorized() {
        jhTmAccount.setJhName("奴风灼烨专卖店");
        JSONObject jsonObject = callTaoApiService.getItemsAuthorized(jhTmAccount);
        JSONArray jsonArray = jsonObject.getJSONArray("brands");
        JSONObject brand = ((JSONObject) jsonArray.get(0));
        Assert.assertEquals("奴风", brand.getString("name"));
        Assert.assertEquals("105881474", brand.getString("vid"));
    }

    @Test
    public void getAddProductSchema() {
        String schema = callTaoApiService.getAddProductSchema(jhTmAccount, 121370008L);
        System.out.println(schema);
    }

    @Test
    public void getItemsOnSale() {
        JSONArray jsonObject = callTaoApiService.getItemsOnSale(jhTmAccount,1L, "sold_quantity:desc");
        System.out.println(jsonObject.toJSONString());
    }

    @Test
    public void upShelfItems() {
        JSONObject jsonObject = callTaoApiService.upShelfItems(jhTmAccount, 959477707441L);
        Assert.assertEquals("success", jsonObject.getString("result"));
    }

    @Test
    public void downShelfItems() {
        JSONObject jsonObject = callTaoApiService.downShelfItems(jhTmAccount, 918888695892L);
        Assert.assertEquals("success", jsonObject.getString("result"));
    }

    @Test
    public void addProduct() {
        //String data = "<itemParam><field id=\"product_images\" name=\"产品图片\" type=\"complex\"><fields><field id=\"product_image_0\" name=\"产品主图\" type=\"input\"><value>https://img.alicdn.com/imgextra/i1/2202796668421/O1CN01yduyGg2C4sF8RFSBi_!!2-item_pic.png</value></field><field id=\"product_image_1\" name=\"产品图片2\" type=\"input\"><value>https://img.alicdn.com/imgextra/i1/2202796668421/O1CN01T4kl5O2C4sF5EITfw_!!2202796668421.png</value></field><field id=\"product_image_2\" name=\"产品图片3\" type=\"input\"><value>https://img.alicdn.com/imgextra/i1/2202796668421/O1CN013Uihir2C4sF7blPLu_!!2202796668421.png</value></field><field id=\"product_image_3\" name=\"产品图片4\" type=\"input\"><value>https://img.alicdn.com/imgextra/i2/2202796668421/O1CN01es6AYs2C4sF3PWpYq_!!2202796668421.png</value></field><field id=\"product_image_4\" name=\"产品图片5\" type=\"input\"><value>https://img.alicdn.com/imgextra/i1/2202796668421/O1CN01EOyPhv2C4sF7olh11_!!2202796668421.png</value></field></fields></field><field id=\"prop_13021751\" name=\"货号\" type=\"input\"><value>jh20250419-1</value></field><field id=\"prop_2631956\" name=\"商品类型\" type=\"singleCheck\"><value>20407737</value></field><field id=\"prop_7699510\" name=\"围裙分类\" type=\"singleCheck\"><value>74716666</value></field><field id=\"prop_20396\" name=\"用途\" type=\"singleCheck\"><value>22046269</value></field><field id=\"infos\" name=\"信息\" type=\"label\"/><field id=\"prop_20509\" name=\"尺码\" type=\"singleCheck\"><value>28383</value></field><field id=\"prop_20608\" name=\"风格\" type=\"singleCheck\"><value>118168</value></field><field id=\"prop_20021\" name=\"材质\" type=\"singleCheck\"><value>28401</value></field></itemParam>";
        String data = "<itemParam><field id=\"product_images\" name=\"产品图片\" type=\"complex\"><fields><field id=\"product_image_4\" type=\"input\"><value>https://img.alicdn.com/imgextra/i2/2219533779671/O1CN01u9PxPX2LJNP82Q1Sl_!!2219533779671.png</value></field><field id=\"product_image_1\" type=\"input\"><value>https://img.alicdn.com/imgextra/i3/2219533779671/O1CN01sgaaFM2LJNPATPsur_!!2219533779671.png</value></field><field id=\"product_image_2\" type=\"input\"><value>https://img.alicdn.com/imgextra/i3/2219533779671/O1CN01LepAT42LJNPAU2ish_!!2219533779671.png</value></field><field id=\"product_image_3\" type=\"input\"><value>https://img.alicdn.com/imgextra/i3/2219533779671/O1CN01DfVdyJ2LJNPBrxslw_!!2219533779671.png</value></field><field id=\"product_image_0\" type=\"input\"><value>https://img.alicdn.com/imgextra/i4/2219533779671/O1CN01ieuQnp2LJNPATUNle_!!2219533779671.png</value></field></fields></field><field id=\"prop_13021751\" name=\"货号\" type=\"input\"><value>jh20250712-1</value></field><field id=\"prop_2631956\" name=\"商品类型\" type=\"singleCheck\"><value>20407737</value></field><field id=\"prop_7699510\" name=\"围裙分类\" type=\"singleCheck\"><value>74716666</value></field><field id=\"prop_20000\" name=\"品牌\" type=\"singleCheck\"/><field id=\"prop_20396\" name=\"用途\" type=\"singleCheck\"><value>22046269</value></field><field id=\"infos\" name=\"信息\" type=\"label\"/><field id=\"prop_20509\" name=\"尺码\" type=\"singleCheck\"><value>28383</value></field><field id=\"prop_20608\" name=\"风格\" type=\"singleCheck\"><value>118168</value></field><field id=\"prop_20021\" name=\"材质\" type=\"singleCheck\"><value>28401</value></field></itemParam>";
        JSONObject jsonObject = callTaoApiService.addProduct(jhTmAccount, 121370008L, data);
        //Assert.assertTrue(jsonObject.getString("add_product_result").contains("8517954183"));
        // error
        Assert.assertTrue(jsonObject.getString("sub_msg").contains("8517954183"));
    }

    @Test
    public void getProductSchema() throws TopSchemaException {
        String jsonObject = callTaoApiService.getProductSchema(jhTmAccount, 8517954183L);
        Assert.assertTrue(jsonObject.contains("可以发布商品"));
    }

    @Test
    public void querySalesVolume() {
        JSONObject jsonObject = callTaoApiService.querySalesVolume(jhTmAccount, 950533647840L);
        Assert.assertEquals("0", jsonObject.getString("sold_quantity"));
    }

    @Test
    // 测试完去回收站把商品还原
    public void deleteItem() {
        JSONObject jsonObject = callTaoApiService.deleteItem(jhTmAccount, 918888695892L);
        Assert.assertEquals("success", jsonObject.getString("result"));
    }

    @Test
    public void getSubmitItemSchema() {
        String jsonObject = callTaoApiService.getSubmitItemSchema(jhTmAccount, 121370008L, 8517954183L);
        Assert.assertTrue(jsonObject.contains("此为开发者消息，在第三方开发者有UI对客户展示时应主动屏蔽此消息"));
    }

    @Test
    public void uploadImage() throws IOException {
        File file = new File("C:\\Users\\86188\\Desktop\\jihuo\\2333.jpg");
        JSONObject jsonObject = callTaoApiService.uploadImage(jhTmAccount, file);
        Assert.assertTrue(jsonObject.getString("picture").contains("picture_id"));
    }

    @Test
    public void uploadImage2() throws IOException {
        String sourceUrl = "https://img.alicdn.com/imgextra/i2/2219533779671/O1CN01PJT01S2LJNPB4fZP4_!!2219533779671.png";
        String newPicUrl = uploadImageApi.uploadImage(sourceUrl);
        System.out.println(newPicUrl);
    }

    @Test
    public void submitItem() throws IOException {
        String itemId = callTaoApiService.submitItem(jhTmAccount, 121370008L, 8517954183L, TestSchema2.main());
        System.out.println(itemId);
        Assert.assertFalse(itemId.isEmpty());
    }


}
